package com.itheima.service.db.soul;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.itheima.mapper.soul.OptionsMapper;
import com.itheima.mapper.soul.TatleMapper;
import com.itheima.soul.Title;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class TitleServiceImpl implements TitleService {

    @Autowired
    TatleMapper tatleMapper;
    @Autowired
    OptionsMapper optionsMapper;


    @Override
    public PageBeanVo findTitleAll(Integer pageNum, Integer pageSize, Integer cid,String keyword) {

        Page<Title> page = new Page<>(pageNum,pageSize);

        QueryWrapper<Title> wrapper = new QueryWrapper<>();

        if (cid!=null) {
            wrapper.eq("cid",cid);
        }

        if (StrUtil.isNotBlank(keyword)) {

            wrapper.like("title",keyword);

        }

        Page<Title> titlePage = tatleMapper.selectPage(page, wrapper);

        return new PageBeanVo(pageNum,pageSize,titlePage.getTotal(),titlePage.getRecords());
    }

    @Override
    public Title findById(Long titleId) {

        QueryWrapper<Title> wrapper = new QueryWrapper<>();

        wrapper.eq("id",titleId);

        Title title = tatleMapper.selectOne(wrapper);

        return title;
    }

    @Override
    public void save(Title title) {

        tatleMapper.insert(title);

        System.out.println(title.getId());


    }

    @Override
    public void update(Title title) {
        tatleMapper.updateById(title);
    }

    @Override
    public Title findByTitle(String titleName) {

        QueryWrapper<Title> wrapper = new QueryWrapper<>();

        wrapper.eq("title",titleName);

        Title title = tatleMapper.selectOne(wrapper);

        return title;
    }

    @Override
    public void delete(Long titleId) {
        QueryWrapper<Title> wrapper = new QueryWrapper<>();

        wrapper.eq("id",titleId);
        tatleMapper.delete(wrapper);

    }
}
